#include "setting.h"

Setting::Setting()
{
    setDefaultValue();    //设置全局默认值
    isDirExist(QCoreApplication::applicationDirPath()+"\\"+"Configure");
}

bool Setting::isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
        return true;
    }
    else
    {
        bool ok = dir.mkdir(fullPath);//只创建一级子目录，即必须保证上级目录存在
        return ok;
    }
}

void Setting::init(QString name)
{
    ConfigureName=name;
    ConfigureFilepath=QCoreApplication::applicationDirPath()+"\\"+"Configure"+"\\"+name+".ini";
    set=QSharedPointer<QSettings>(new QSettings(ConfigureFilepath,QSettings::IniFormat));
}


bool Setting::ConfigurExist(QString path)
{
    QFileInfo file(ConfigureFilepath);
    if(file.exists())
    {
        return true;
    }
    else
    {
        return false;
    }
}


void Setting::Start()
{
    if(ConfigurExist(ConfigureFilepath))
    {
        qDebug()<<"找到配置文件!";
        ReadConfigure();
        qDebug()<<"设置参数完毕";
    }
    else
    {
        qDebug()<<"未找到配置文件,使用默认配置";
        SetDefaultConfigur(); //设置配置默认值
    }
}


void Setting::setDefaultValue()
{
    Common::ConfigureLock.lockForWrite();
    Common::Log_Open="true";
    Common::Log_Size=500;
    Common::Tcp_ip="127.0.0.1";
    Common::Tcp_port=60000;
    Common::LocalPort="localCaculate";
    Common::Hand_Count=1;
    Common::Caculate_Count=-1;
    Common::ConfigureLock.unlock();
}



void Setting::ReadConfigure()
{
    Common::ConfigureLock.lockForWrite();

    if(!set.data()->value("Log/Log_Open").isNull())
    {
        Common::Log_Open=set.data()->value("Log/Log_Open").toBool();
    }
    if(!set.data()->value("Tcp/Tcp_ip").isNull())
    {
        Common::Tcp_ip=set.data()->value("Tcp/Tcp_ip").toString();
    }
    if(!set.data()->value("LocalServer/Port").isNull())
    {
        Common::LocalPort=set.data()->value("LocalServer/Port").toString();
    }

    if(!set.data()->value("Tcp/Port").isNull())
    {
        bool ok;
        int Port=set.data()->value("Tcp/Port").toInt(&ok);
        if(ok)
        {
            Common::Tcp_port=Port;
        }
    }
    if(!set.data()->value("Log/Log_Size").isNull())
    {
        bool ok;
        int Log_Size=set.data()->value("Log/Log_Size").toInt(&ok);
        if(ok)
        {
            Common::Log_Size=Log_Size;
        }
    }
    if(!set.data()->value("Handle/Handle-Count").isNull())
    {
        bool ok;
        int Handle_Count=set.data()->value("Handle/Handle-Count").toInt(&ok);
        if(ok)
        {
            Common::Hand_Count=Handle_Count;
        }
    }
    if(!set.data()->value("Handle/Caculate-Count").isNull())
    {
        bool ok;
        int Caculate_Count=set.data()->value("Handle/Caculate-Count").toInt(&ok);
        if(ok)
        {
            Common::Caculate_Count=Caculate_Count;
        }
    }
    Common::ConfigureLock.unlock();
}

void Setting::SetDefaultConfigur()
{
    set.data()->setValue("Log/Log_Open","true");
    set.data()->setValue("Log/Log_Size","500");
    set.data()->setValue("Tcp/Port","60000");
    set.data()->setValue("Tcp/Tcp_ip","127.0.0.1");
    set.data()->setValue("LocalServer/Port","localCaculate");
    set.data()->setValue("Handle/Handle-Count","1");
    set.data()->setValue("Handle/Caculate-Count","-1");
}


